/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.myfaces.test.webapp.impl;

import org.apache.myfaces.test.webapp.impl.metadata.assertion.AssertableELExpression;
import org.apache.myfaces.test.webapp.impl.metadata.assertion.AssertableProxyMethod;
import org.apache.myfaces.test.webapp.impl.metadata.assertion.AssertableValue;
import org.apache.myfaces.test.webapp.impl.metadata.assertion.TestAssertExpectCall;

import javax.el.*;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import java.beans.FeatureDescriptor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Cosmin Martinconi, 11.08.2010
 */
public class ELResolverWrapper extends ELResolver {

    private List<TestAssertExpectCall> expectCall = new ArrayList<TestAssertExpectCall>();
    private Application application;

    public ELResolverWrapper(Application application) {
        this.application = application;
    }

    @Override
    public Object getValue(ELContext context, Object base, Object property) {

        for (TestAssertExpectCall taec : expectCall) {
            if (validateCallExpected(base, property, taec.getAssertableValue()))
                taec.setCalled(true);
        }

        return getWrapped().getValue(context, base, property);
    }

    @Override
    public void setValue(ELContext context, Object base, Object property, Object value) {

        for (TestAssertExpectCall taec : expectCall) {
            taec.setCalled(validateCallExpected(base, property, taec.getAssertableValue()));
        }

        getWrapped().setValue(context, base, property, value);
    }

    @Override
    public Class<?> getType(ELContext context, Object base, Object property) {
        return getWrapped().getType(context, base, property);
    }

    @Override
    public boolean isReadOnly(ELContext context, Object base, Object property) {
        return getWrapped().isReadOnly(context, base, property);
    }

    @Override
    public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
        return getWrapped().getFeatureDescriptors(context, base);
    }

    @Override
    public Class<?> getCommonPropertyType(ELContext context, Object base) {
        return getWrapped().getCommonPropertyType(context, base);
    }

    private ELResolver getWrapped() {
        return application.getELResolver();
    }


    private boolean validateCallExpected(Object base, Object property, AssertableValue assertableValue) {

        if ((assertableValue == null) || (base == null))
            return false;

        FacesContext fc = FacesContext.getCurrentInstance();
        if (assertableValue instanceof AssertableELExpression) {
            AssertableELExpression elValue = (AssertableELExpression)assertableValue;

            Object assertBase = getValue(fc.getELContext(), null, elValue.getELBaseAsString());

            return base.equals(assertBase) &&
                    property.equals(elValue.getELPropertyAsString());
        }

        if (assertableValue instanceof AssertableProxyMethod) {
            AssertableProxyMethod methodValue = (AssertableProxyMethod)assertableValue;

            String firstUPProperty = Character.toUpperCase(property.toString().charAt(0)) + property.toString().substring(1);
            String assertPropertyName = methodValue.getMethodProxy().getSignature().getName();

            return base.getClass().equals(methodValue.getMethodObj().getClass().getSuperclass()) &&
                    assertPropertyName.matches("((get)|(set))" + firstUPProperty);
        }

        return false;
    }

    public void addExpectCall(TestAssertExpectCall eCall) {
        expectCall.add(eCall);
    }

    public void removeExpectCall(TestAssertExpectCall eCall) {
        expectCall.remove(eCall);
    }
}
